package com.bqtt.tumor.demo.business;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bqtt.tumor.demo.common.MyBeanFactory;
import com.bqtt.tumor.demo.common.static_struct.FunctionList;
import com.bqtt.tumor.demo.data_access.HealthUserDAO;
import com.bqtt.tumor.demo.data_access.SystemUserDAO;
import com.bqtt.tumor.demo.entity.FunctionListItem;
import com.bqtt.tumor.demo.entity.HealthUser;
import com.bqtt.tumor.demo.entity.SystemUser;

public class HealthUserService {

	
	private static Logger log = LoggerFactory.getLogger(HealthUserService.class);
	
	private static HealthUserDAO healthUserDAO = (HealthUserDAO) MyBeanFactory.getBean("healthUserDAO");
	
	
	public static List<FunctionListItem> getFunctionList(SystemUser user,String currentPage)
	{
		List<FunctionListItem> result  = FunctionList.getListByPrivilege(user.getPrivilege());
		for(FunctionListItem item: result )
		{
			if(item.itemName.equals(currentPage))
			{
				item.cssText = "selected";
			}
		}
		return result;
	}

	public static List<HealthUser> searchByUserCondition(HealthUser condition,int pageSize,int pageNo)
	{
		
		List<HealthUser> result = healthUserDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);
		
		for(HealthUser item:result)
		{
			item.fillStringField();
		}
		return result;
	}
	
	
	public static List<HealthUser> searchByUserCondition(HealthUser condition,int pageSize,int pageNo,long defaultShowId)
	{
		
		List<HealthUser> result = healthUserDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);
		
		for(HealthUser item:result)
		{
			item.fillStringField();
			if(item.getHealth_user_id()==defaultShowId)
			{
				item.setDisplayName("inline-block");
			}
		}
		return result;
	}
	
	public static HealthUser getUserByIdNumber(String id_number)
	{
		List<HealthUser> result = healthUserDAO.findByProperty("id_number", id_number);
		
		if(result!=null && result.size()>0)
		{
			return result.get(0);
		}
		
		return null;
	}
	
	public static boolean addUser(HealthUser user)
	{
		try
		{
			user.fillDBfields();
			healthUserDAO.save(user);
			return true;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}
	public static boolean updateUser(HealthUser user)
	{
		try
		{
			user.fillDBfields();
			if(user.getHealth_user_id()>0)
			{
				healthUserDAO.update(user);
				return true;
			}
			else
				
			return false;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}

	public static HealthUserDAO getHealthUserDAO() {
		return healthUserDAO;
	}

	public static void setHealthUserDAO(HealthUserDAO healthUserDAO) {
		HealthUserService.healthUserDAO = healthUserDAO;
	}
	
	
}
